package com.winsdom.controller;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.domain.vo.TopRoledVO;
import com.winsdom.dto.RoleDTO;
import com.winsdom.dto.RoleManageDto;
import com.winsdom.entity.SysFatherPermission;
import com.winsdom.entity.SysPermission;
import com.winsdom.entity.SysRolePermission;
import com.winsdom.enums.AuthenticationType;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.service.ISysPermissionService;
import com.winsdom.service.ISysRolePermissionService;
import com.winsdom.service.ISysRoleService;
import com.winsdom.service.SysFatherPremissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@Authentication(value = AuthenticationType.ROLE_MANAGEMENT)
public class RoleController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysFatherPremissionService sysFatherPremissionService;
    @Autowired
    private ISysRolePermissionService iSysRolePermissionService;



    /*
      * 条件查询分页角色信息
      * @param roleDTO
      * @return
     */
    @PostMapping("/listBatch")
    @ApiOperation("条件查询分页角色信息")
    @OperationLog(description = "条件查询分页角色信息")
    public Result<PageResult> findRolesBatch(@Validated @RequestBody RoleDTO roleDTO) {
        log.info("分页查询角色信息的条件：{}", roleDTO);
        PageResult page = roleService.findRolesBatch(roleDTO);
        return Result.success(page);
    }


    /*
     * 获取权限列表
     *
     * @return
     */
    @GetMapping("/getrolelist")
    @ApiOperation("获取权限列表")
    @OperationLog(description = "获取权限列表")
    public Result getRole() {
        List<TopRoledVO> list = sysFatherPremissionService.getListRole();
        return Result.success(list);
    }


    /*
     * 获取修改人的权限
     *
     * @return
     */
    @GetMapping("/getrolePermissionlist/{id}")
    @ApiOperation("获取角色权限")
    @OperationLog(description = "获取角色权限")
    public Result getRolePermission(@PathVariable Long id) {
        List<SysRolePermission> list =iSysRolePermissionService.listSysRolePermission(id);
        return Result.success(list);
    }


    /*
     * 修改权限
     * @param sysFatherPermission
     * @return
     */
    @PostMapping("/updaterole")
    @ApiOperation("修改角色权限")
    @OperationLog(description = "修改角色权限")
    public Result updateRole(@RequestBody RoleManageDto roleManageDto) {
        log.info("修改角色权限dto:{}",roleManageDto);
        if (roleManageDto.getId() == null) {
            return Result.error("角色id不能为空");
        } else {
            String s = sysFatherPremissionService.selectRoleName(roleManageDto.getId(),roleManageDto.getRoleName());
            if (s!= null && !s.equals("")){
                return Result.error("角色名已存在");
            }
            Long roleId = sysFatherPremissionService.selectRoleId(roleManageDto.getId());
            if (roleId == null) {
                return Result.error("角色ID不存在");
            }
            List<RoleManageDto> list = sysFatherPremissionService.updateRole(roleManageDto);
            if (list.size() == 0) {
                return Result.error("修改失败");
            } else {
                return Result.success("修改成功");
            }
        }
    }

    /*
     * 新增角色
     * @param sysFatherPermission
     * @return
     */
    @PostMapping("/addRole")
    @ApiOperation(value = "增加角色")
    @OperationLog(description = "添加角色")
    public Result addRole(@RequestBody RoleManageDto addRoleDTO){
        if(addRoleDTO.getRoleName()==null || "".equals(addRoleDTO.getRoleName()))
            return Result.error("角色名不能为空");
        if(roleService.addRole(addRoleDTO)==0){
            return Result.error("角色名已存在");
        }
        return Result.success(roleService.addRole(addRoleDTO));
    }

    /*
     * 禁用角色
     * @param roleId
     * @return
     */
    @GetMapping("/disableRole")
    @ApiOperation(value = "禁用角色")
    @OperationLog(description = "禁用角色")
    public Result disableRole(@RequestParam Long roleId) {
        if (roleId == null) {
            return Result.error("角色id不能为空");
        } else {
            Long roleId1 = sysFatherPremissionService.selectRoleId(roleId);
            if (roleId1 == null) {
                return Result.error("角色ID不存在");
            }
            int roleResult = roleService.disableRole(roleId);
            if (roleResult == 0) {
                return Result.error("已有账号关联，禁用失败");
            } else {
                return Result.success("禁用成功");
            }
        }
    }

    /*
     * 启用角色
     * @param roleId
     * @return
     */
    @GetMapping("/enableRole")
    @ApiOperation(value = "启用角色")
    @OperationLog(description = "启用角色")
    public Result enableRole(@RequestParam Long roleId) {
        if (roleId == null) {
            return Result.error("角色id不能为空");
        } else {
            Long roleId2= sysFatherPremissionService.selectRoleId(roleId);
            if (roleId2 == null) {
                return Result.error("角色ID不存在");
            }
            Long roleResult = roleService.enableRole(roleId);
            if (roleResult == 0) {
                return Result.error("启用失败");
            } else {
                return Result.success("启用成功");
            }
        }
    }

}